﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using Terraria.UI;

namespace Terraria.GameContent.UI.Elements
{
	// Token: 0x020003FD RID: 1021
	public class UITextBox : UITextPanel<string>
	{
		// Token: 0x060023DF RID: 9183 RVA: 0x000195BD File Offset: 0x000177BD
		public UITextBox(string text, float textScale = 1f, bool large = false) : base(text, textScale, large)
		{
		}

		// Token: 0x060023E0 RID: 9184 RVA: 0x000195D0 File Offset: 0x000177D0
		public void Write(string text)
		{
			base.SetText(base.Text.Insert(this._cursor, text));
			this._cursor += text.Length;
		}

		// Token: 0x060023E1 RID: 9185 RVA: 0x0047ABDC File Offset: 0x00478DDC
		public override void SetText(string text, float textScale, bool large)
		{
			if (text.ToString().Length > this._maxLength)
			{
				text = text.ToString().Substring(0, this._maxLength);
			}
			base.SetText(text, textScale, large);
			this._cursor = Math.Min(base.Text.Length, this._cursor);
		}

		// Token: 0x060023E2 RID: 9186 RVA: 0x000195FD File Offset: 0x000177FD
		public void SetTextMaxLength(int maxLength)
		{
			this._maxLength = maxLength;
		}

		// Token: 0x060023E3 RID: 9187 RVA: 0x00019606 File Offset: 0x00017806
		public void Backspace()
		{
			if (this._cursor == 0)
			{
				return;
			}
			base.SetText(base.Text.Substring(0, base.Text.Length - 1));
		}

		// Token: 0x060023E4 RID: 9188 RVA: 0x00019630 File Offset: 0x00017830
		public void CursorLeft()
		{
			if (this._cursor == 0)
			{
				return;
			}
			this._cursor--;
		}

		// Token: 0x060023E5 RID: 9189 RVA: 0x00019649 File Offset: 0x00017849
		public void CursorRight()
		{
			if (this._cursor < base.Text.Length)
			{
				this._cursor++;
			}
		}

		// Token: 0x060023E6 RID: 9190 RVA: 0x0047AC38 File Offset: 0x00478E38
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			this._cursor = base.Text.Length;
			base.DrawSelf(spriteBatch);
			this._frameCount++;
			if ((this._frameCount %= 40) > 20)
			{
				return;
			}
			CalculatedStyle innerDimensions = base.GetInnerDimensions();
			Vector2 pos = innerDimensions.Position();
			DynamicSpriteFont dynamicSpriteFont = base.IsLarge ? Main.fontDeathText : Main.fontMouseText;
			Vector2 vector = new Vector2(dynamicSpriteFont.MeasureString(base.Text.Substring(0, this._cursor)).X, base.IsLarge ? 32f : 16f) * base.TextScale;
			if (base.IsLarge)
			{
				pos.Y -= 8f * base.TextScale;
			}
			else
			{
				pos.Y += 2f * base.TextScale;
			}
			pos.X += (innerDimensions.Width - base.TextSize.X) * 0.5f + vector.X - (base.IsLarge ? 8f : 4f) * base.TextScale + 6f;
			if (base.IsLarge)
			{
				Utils.DrawBorderStringBig(spriteBatch, "|", pos, base.TextColor, base.TextScale, 0f, 0f, -1);
				return;
			}
			Utils.DrawBorderString(spriteBatch, "|", pos, base.TextColor, base.TextScale, 0f, 0f, -1);
		}

		// Token: 0x04004003 RID: 16387
		private int _cursor;

		// Token: 0x04004004 RID: 16388
		private int _frameCount;

		// Token: 0x04004005 RID: 16389
		private int _maxLength = 20;
	}
}
